package com.ocom.emqx.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.ocom.common.constant.EmqxUrl;
import com.ocom.common.entity.emqx.EntityYhMqtt86;
import com.ocom.common.entity.emqx.EntityYhMqtt8A;
import com.ocom.common.entity.mongo.EntityDeviceHistoryPo;
import com.ocom.common.entity.payconfig.EntityMqttDevice;
import com.ocom.common.request.emqx.YhMqtt0606Request;
import com.ocom.common.request.emqx.YhMqtt0607Request;
import com.ocom.common.request.payhandle.QrCardPayRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.emqx.client.PayconfigFeignService;
import com.ocom.emqx.handle.MqttSendClient;
import com.ocom.emqx.service.WashingMachineService;
import com.ocom.emqx.timer.WashingMachineTask;
import com.ocom.emqx.util.EmqxApiUtil;
import com.ocom.emqx.util.EmqxConvert;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
public class WashingMahineController {


    @Resource
    private RedisUtil redisUtil;

    @Autowired
    PayconfigFeignService payconfigFeignService;


    @Autowired
    MqttSendClient mqttSendClient;

    @Autowired
    private WashingMachineService washingMachineService;
    @Autowired
    private WashingMachineTask washingMachineTask;

    @PostMapping(EmqxUrl.URL_perOrder)
    public Result perOrder(@RequestBody QrCardPayRequest request) {


        //1.发送 06
//        String comm06 = EmqxApiUtil.washing0607(RandomUtil.randomNumbers(9),52l);
//        String comm06 = EmqxApiUtil.washing0607("00000005",52l);
//        mqttSendClient.publish(false,  "ws/cm/"+request.getMachNo() , "0400015301" );
//        String mqttDevice = "YH2408173830";
//        YhMqtt0607Request yhMqtt0607Request = new YhMqtt0607Request();
//        yhMqtt0607Request.setOrderNo(RandomUtil.randomLong(1,100));
//        yhMqtt0607Request.setLaundryModel(6);
//        yhMqtt0607Request.setDeviceNo("YH2408173830");
//        washingMachineService.yHMqtt0607(yhMqtt0607Request);
//        mqttSendClient.publish(false,  "ws/cm/"+mqttDevice , comm06 );
//        YhMqtt0606Request yhMqtt0606Request = new YhMqtt0606Request();
//        yhMqtt0606Request.setOrderNo(RandomUtil.randomLong(1,10));
//        yhMqtt0606Request.setDeviceNo("YH2408175629");
//        yhMqtt0606Request.setOverTime(BigDecimal.ONE);
//        yhMqtt0606Request.setTotalTime(2);
//        washingMachineService.yHMqtt0606(yhMqtt0606Request);
        //
        String deviceNo = "YH2408173830";
//        String msg = "84000D539C283B767D544A283B767C535F";
        String msg = "84000D5471283B767D547B283B767C531A";
        washingMachineService.updateMachIneStatusWd(deviceNo,msg);
        //2.接收06
//        washingMachineService.resetOrderStatus();
        return null;

    }

    @GetMapping("/test001")
    public Result test01(String deviceNo){
        mqttSendClient.publish(false, deviceNo , "0400015301" );
        return ResultUtil.success();
//        chageState.add(topicVo.getTopic().replace("ws/cm/",""));
    }


    @GetMapping("/test002")
    public Result test02(String deviceNo,String msg){
        washingMachineService.updateMachIneStatusWd(deviceNo,msg);
        return ResultUtil.success();
//        chageState.add(topicVo.getTopic().replace("ws/cm/",""));
    }

    @GetMapping("/test003")
    public Result test(Integer status,Integer subRun,Integer subNum){

        return ResultUtil.success(washingMachineService.yhDeviceWorkStatus(status,subRun,subNum));
    }

    @GetMapping("/test004")
    public Result test04(Integer status,Integer subRun){
        return ResultUtil.success(washingMachineService.yHStatus(status,subRun));
    }

    @GetMapping("/test005")
    public Result test04(){
        washingMachineTask.scheduled1();
        return ResultUtil.success();
    }

    @GetMapping("/test006")
    public Result test8A(String msg,String deviceNo){
        EntityYhMqtt8A entityYhMqtt8A  = EmqxConvert.convertYhMqtt8A(msg);
        Result<EntityMqttDevice> result = payconfigFeignService.getMqttDeviceInfo(deviceNo);
        EntityDeviceHistoryPo deviceHistoryPo = Convert.convert(EntityDeviceHistoryPo.class,entityYhMqtt8A);
        EntityMqttDevice entityMqttDevice = result.getData();
        washingMachineService.updateDeviceStatus(entityMqttDevice,deviceHistoryPo);
        return ResultUtil.success();
    }
}